import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UploadedFile,
  Req,
  UseGuards,
  Query,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
  ApiBearerAuth,
  ApiBody,
  ApiConsumes,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { RolesGuard } from '../../guards/roles.guard';
import { PermissionsGuard } from '../../guards/permissions.guard';
import { FileService } from './file.service';
import { CreateFileDto, FileResponseDto, UpdateFileDto } from './dto';
import { Permissions } from '../../decorators/permissions.decorator';


@ApiTags('文件管理')
@Controller('files')
@UseGuards(JwtAuthGuard, RolesGuard, PermissionsGuard)
@ApiBearerAuth()
export class FileController {
  constructor(private readonly fileService: FileService) {}

  @Post()
  @ApiOperation({ summary: '上传文件' })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: '要上传的文件',
        },
        description: {
          type: 'string',
          description: '文件描述',
        },
        taskId: {
          type: 'string',
          format: 'uuid',
          description: '关联的任务ID（可选）',
        },
      },
    },
  })
  @ApiResponse({
    status: 201,
    description: '文件上传成功',
    type: FileResponseDto,
  })
  @UseInterceptors(FileInterceptor('file'))
  @Permissions('files:create')
  async uploadFile(
    @UploadedFile() file: Express.Multer.File,
    @Body() createFileDto: CreateFileDto,
    @Req() req: any,
  ) {
    return this.fileService.create(file, createFileDto, req.user.userId);
  }

  @Get()
  @ApiOperation({ summary: '获取所有文件' })
  @ApiResponse({
    status: 200,
    description: '返回文件列表',
    type: [FileResponseDto],
  })
  @Permissions('files:read')
  findAll() {
    return this.fileService.findAll();
  }

  @Get('task/:taskId')
  @ApiOperation({ summary: '获取任务的所有相关文件' })
  @ApiParam({
    name: 'taskId',
    description: '任务ID',
    type: String,
  })
  @ApiResponse({
    status: 200,
    description: '返回文件列表',
    type: [FileResponseDto],
  })
  @Permissions('files:read')
  findByTask(@Param('taskId') taskId: string) {
    return this.fileService.findByTask(taskId);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取单个文件信息' })
  @ApiParam({
    name: 'id',
    description: '文件ID',
    type: String,
  })
  @ApiResponse({
    status: 200,
    description: '返回文件信息',
    type: FileResponseDto,
  })
  @Permissions('files:read')
  findOne(@Param('id') id: string) {
    return this.fileService.findOne(id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新文件信息' })
  @ApiParam({
    name: 'id',
    description: '文件ID',
    type: String,
  })
  @ApiResponse({
    status: 200,
    description: '文件信息更新成功',
    type: FileResponseDto,
  })
  @Permissions('files:update')
  update(@Param('id') id: string, @Body() updateFileDto: UpdateFileDto) {
    return this.fileService.update(id, updateFileDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除文件' })
  @ApiParam({
    name: 'id',
    description: '文件ID',
    type: String,
  })
  @ApiResponse({
    status: 200,
    description: '文件删除成功',
  })
  @Permissions('files:delete')
  remove(@Param('id') id: string) {
    return this.fileService.remove(id);
  }
}
